package com.yztc.yidoutang.fragment.zhuye_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.stx.xhb.xbanner.XBanner;
import com.yztc.yidoutang.R;
import com.yztc.yidoutang.base.BaseFragment;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 主页Fragment页面
 */
public class ZhuYeFragment extends BaseFragment implements ZhuYeContract.View {
    private XBanner banner;
    private ImageView cardBackground;
    private CircleImageView header;
    private TextView title;
    private TextView author;
    private Button more;
    private CheckBox change;
    private TabLayout tabs;
    private ViewPager vp;

    private ZhuYePresenter presenter = new ZhuYePresenter(this);

    @Override
    public int getLayoutId() {
        return R.layout.fragment_zhuye;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        banner = (XBanner) view.findViewById(R.id.find_zhuye_banner_banner);
        cardBackground = (ImageView) view.findViewById(R.id.find_zhuye_iv_card);
        header = (CircleImageView) view.findViewById(R.id.find_zhuye_iv_header);
        title = (TextView) view.findViewById(R.id.find_zhuye_tv_title);
        author = (TextView) view.findViewById(R.id.find_zhuye_tv_author);
        more = (Button) view.findViewById(R.id.find_zhuye_btn_more);
        change = (CheckBox) view.findViewById(R.id.find_zhuye_cb_change);
        tabs = (TabLayout) view.findViewById(R.id.find_zhuye_tab_tabs);
        vp = (ViewPager) view.findViewById(R.id.find_zhuye_vp_vp);
        initViews();
        initEvents();
    }

    /**
     * 初始化界面布局
     */
    private void initViews() {
        // 从网络中获取Banner、CardView和Tab中的数据并进行适配
        initBaseData();
    }

    /**
     * 初始化事件
     */
    private void initEvents() {
        // 当点击“交换”按钮时改变RecyclerView的展示样式
        change.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                onChangeClicked(isChecked);
            }
        });
    }

    @Override
    public void initBaseData() {
        presenter.initBaseData(getActivity(), banner, cardBackground, header, title, author, tabs, vp);
    }

    @Override
    public void onChangeClicked(boolean isChecked) {
        presenter.onChangeClicked(isChecked);
    }
}